// @ 516
// _(:з」∠)_

define([], function() {
    'use strict';

    const MC = {
        map: null,
        baseMap: {},
        mapLayer: [],
        mapEvent: {},
        bindEvent: null,
        mapMarker: [],
        mapDisMarker: [],
        mapDisMarker1: [],
        mapCircle: [],
        mapPolyline: [],
        mapPolygon: [],
        mapPopup: [],
        mapStation: [],
        mapAllLayer: [],
        mapToolEvent: false,
        China_Albers: "+proj=aea +lat_0=0 +lat_1=25 +lat_2=47 +lon_0=95 +x_0=0 +y_0=0 +k=1 +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
        customCRS: null
    };

    function initMap() {
        let originResult = {
                "top":1.651523213071033E7,
                "left":-1.6918978072656643E7,
                "bottom":-4771706.39258473,
                "leftBottom": {
                    "x":-1.6918978072656643E7,
                    "y":-4771706.39258473
                },
                "right":1.6918978072656643E7,
                "rightTop":{
                    "x":1.6918978072656643E7,
                    "y":1.651523213071033E7
                }
            },
            visableResolution = [
                156543.0339280409,78271.51696402059,
                39135.75848201029,19567.879241005117,
                9783.939620502559,4891.969810251267,
                2445.9849051256388,1222.9924525628205,
                611.4962262814089,305.7481131407058,
                152.87405657035237,76.43702828517618,
                38.218514142588226,19.10925707129406,
                9.55462853564703
            ];

        MC.customCRS = L.Proj.CRS( "EPSG:3857", {
            bounds: L.bounds([originResult.left, originResult.bottom], [originResult.right, originResult.top]),
            resolutions: visableResolution,
            origin: [originResult.left, originResult.top]
        });
            
        // forward -> 经纬度转albers
        // inverse -> albers转经纬度
        // test - > proj4( "proj" ).forward([ lng, lat ])

        // create map
        MC.map = L.map( 'onemap', {
            center: L.latLng([ 35, 6 ]),
            zoom: 5,
            maxZoom: 18,
            minZoom: 4,
            zoomControl: false,
            zoomDelta: 0.25,
            zoomSnap: 0,
            crs: MC.customCRS
        });

        // load map module
        MC.map["controlZoom"] = L.control.zoom({ position: "bottomright" });
        MC.map["controlScale"] = L.control.scale({ position: "bottomright" });

        // base map
        MC.baseMap = {
            googleMap_mt: L.tileLayer(
                "http://mt2.google.cn/vt/lyrs=p&hl=zh-cn&gl=cn&src=app&x={x}&y={y}&z={z}",
                { id: 'googleMap_mt', noWrap: false, minZoom: 3, maxZoom: 21, attribution: 'Google Map', } ),
            googleMap_khm: L.tileLayer(
                "http://khm3.google.com/kh/v=810&src=app&x={x}&y={y}&z={z}",
                { id: 'googleMap_khm', noWrap: false, minZoom: 3, maxZoom: 21, attribution: 'Google Map', } ),
            wmflabs_bwMapnik: L.tileLayer(
                "http://a.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png",
                { id: 'wmflabs_bwMapnik', noWrap: false, minZoom: 3, maxZoom: 21, attribution: 'wmflabs bw-mapnik', } ),
            wmflabs_osm: L.tileLayer(
                "http://a.tiles.wmflabs.org/osm/{z}/{x}/{y}.png",
                { id: 'wmflabs_osm', noWrap: false, minZoom: 3, maxZoom: 21, attribution: 'wmflabs OSM', } ),
            supermap_rest: L.supermap.tiledMapLayer(
                "http://118.25.235.51:8090/iserver/services/map-China400/rest/maps/China",
                { crs: MC.customCRS, attribution: 'SuperMap' }
            )
        };

        // google lyrs :
        // m：路线图
        // t：地形图
        // ​p：带标签的地形图
        // ​s：卫星图
        // y：带标签的卫星图
        // ​h：标签层（路名、地名等）

        // init map param
        MC.map.addLayer( MC.baseMap.supermap_rest );
        // MC.map.fitBounds([
        //     [ 28.790434611931115, 12.378784615167007 ],
        //     [ 39.58626269400332, 22.314151657528914 ]
        // ]);
        // MC.mapEvent.drawBounds( "/data/bounds/HHH.json", "", "" );
    }

    setTimeout(function() {
        require([ "iclient9-leaflet" ], function() {
            initMap();
        });
        
        let header = document.querySelector(".header"),
            main = document.querySelector(".main"),
            fullScreen = header.querySelector(".fullScreen");
            
        fullScreen.addEventListener("click", function(e) {
            if ( header.hasClass("hide") ) {
                header.removeClass("hide");
                main.style.height = document.body.clientHeight-50+"px";
            } else {
                header.addClass("hide");
                main.style.height = document.body.clientHeight+"px";
            }
        }, false);
    }, 10);

    window.MC = MC;

    return MC;
});